import wepy from 'wepy'
export default class extends wepy.mixin {
	data = {
		selected: 0,
		// 活动id
		activeId: '',
		// 活动数据
		activeInfo: {},
		// 时间段商品列表
		timeInfo: {},
		basUrl: wepy.$instance.globalData.baseURl,
		// 活动名称
		activeName: '',
		// 默认地址数据
		getaddressDeftInfo: [],
		// 用户id
		userId: '',
		// 秒杀订单数数据编号
		orderList: '',
		// 状态码
		state: '',
		biao: 0,
		// 活动开始时间
		starttime: '',
		// 活动结束时间
		endTime: '',
		xianzaiTime: '',
		urls: wepy.$instance.globalData.baseURl,
		zhanweit: '2020-05-07/202005071101421588820502650_d2l0h.png'
	}
	onLoad(options) {
		console.log('秒杀页面已经启动')
		console.log(options)
		this.activeId = options.id
		let userid = wepy.getStorageSync('userId')
		console.log(userid)
		this.userId = userid
	}
	onShow() {
		this.getActiveList()
		// 获取默认地址
		this.getAddressdefalt()
		this.biao = "0"
	}
	methods = {
		handle1(e) {
			console.log(e, "----------------")
			console.log(e.currentTarget.dataset.id)
			this.biao = e.currentTarget.dataset.index
			let index = e.currentTarget.dataset.index
			console.log('状态码', e.currentTarget.dataset.state)
			this.state = e.currentTarget.dataset.state
			console.log('时间啊', e.currentTarget.dataset.stime)
			this.starttime = e.currentTarget.dataset.stime
			this.endTime = e.currentTarget.dataset.etime
			// if(e.currentTarget.dataset.state == 0) {
			//   return wepy.noToast('活动暂未开始或者已经结束..')
			// } 
			console.log(index)
			this.selected = index
			this.getTimeGoods(e.currentTarget.dataset.id)
			let timeId = e.currentTarget.dataset.id
			console.log(timeId)
		},
		// 点击抢购
		async handle2(e) {
			console.log(e, "mmmmmmmmm")
			if (wepy.getStorageSync('userId')) {
				var timestamp = Date.parse(new Date())
				timestamp = timestamp
				this.xianzaiTime = timestamp
				console.log("当前时间戳为：" + timestamp);
				console.log('888', e.target.dataset)
				if (this.state != "2") {
					return wepy.noToast('活动未在进行中')
				} else {
					if (Number(this.starttime) > Number(this.xianzaiTime)) {
						return wepy.noToast('活动未开始')
					} else if (Number(this.xianzaiTime) > Number(this.endTime)) {
						return wepy.noToast('活动已结束')
					} else {
						// 商品id
						let goodsid = e.target.dataset.goodsid
						// 活动关联id
						let hdid = e.target.dataset.id
						this.$parent.globalData.seckillGoodId = hdid;
						wepy.navigateTo({
							url: '../../pages/goodsDatils/gooddatils?id=' + goodsid + '&skuAttrValueIds=' + e.target.dataset.item.productSkuBean
								.attrCodes + '&seckillGoodId=' + hdid,
						})
					}
				}
			} else {
				wx.navigateTo({
					url: '../../pages/Authorization/authorization',
				})
			}
		},
	}
	computed = {
		// 判断页面是否有活动
		isEmpty() {
			if (this.activeInfo.seckillTimeScopeList <= 0) {
				return true
			}
			return false
		}
	}
	js_date_time(unixtime) {
		var date = new Date(unixtime);
		var y = date.getFullYear();
		var m = date.getMonth() + 1;
		m = m < 10 ? ('0' + m) : m;
		var d = date.getDate();
		d = d < 10 ? ('0' + d) : d;
		var h = date.getHours();
		h = h < 10 ? ('0' + h) : h;
		var minute = date.getMinutes();
		var second = date.getSeconds();
		minute = minute < 10 ? ('0' + minute) : minute;
		second = second < 10 ? ('0' + second) : second;
		// return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;//年月日时分秒
		return y + '-' + m + '-' + d + ' ' + h + ':' + minute;

	}
	// 获取秒杀数据
	async getActiveList() {
		const {
			data: res
		} = await wepy.post('baseRest/getSeckillInfo', {
			seckillId: this.activeId,
		})
		console.log('秒杀时间', res)
		if (res.state != 1) {
			return wepy.noToast('网络错误，请稍后重试')
		}
		console.log(res.data)
		this.activeInfo = res.data
		this.state = res.data.seckillTimeScopeList[0].state
		this.starttime = res.data.seckillTimeScopeList[0].startTimes
		this.endTime = res.data.seckillTimeScopeList[0].endTImes
		this.activeName = res.data.actTitle
		this.getTimeGoods(res.data.seckillTimeScopeList[0].id)
		this.$apply()
	}
	// 根据时间段id查询数据
	async getTimeGoods(timeId) {
		const {
			data: res
		} = await wepy.post(
			'baseRest/getSeckillProductBySecTimeId', {
				secTimeId: timeId,
			}
		)
		console.log('时间段id数据', res)
		if (res.state != 1) {
			return wepy.noToast('网络错误，请稍后重试')
		}
		var data = res.data
		for (var i = 0; i < data.content.length; i++) {
			data.content[i].num = 1
		}
		this.timeInfo = data
		console.log(data, "ppppppppppppppp")
		this.$apply()
	}
	// 获取默认收货地址
	async getAddressdefalt() {
		const res = await wepy.post('baseRest/getDefaultAddr', {
			memberId: this.userId,
		})
		console.log('获取默认地址', res)
		if (res.statusCode != 200) {
			wepy.noToast('获取地址失败')
		}
		this.getaddressDeftInfo = res.data.data
		this.$apply()
	}
}
